﻿using MyHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using clf.weighing.entity;

namespace weighing
{
    public class LocalConfigDao : BaseDao<LocalConfig>
    {
        #region instance

        private static LocalConfigDao? instance;
        public static LocalConfigDao Instance => instance ??= new LocalConfigDao();

        public override string GetTableName()
        {
            return TableName.config.ToString();
        }
        #endregion
        /// <summary>
        /// 获取本地的配制项
        /// </summary>
        /// <returns></returns>
        public LocalConfig GetLocalConfig()
        {
            return ConfigDao.Instance.GetLocalConfig();           
        }

        public LocalConfig GetDefault()
        {
            return LocalConfig.Default;
        }

        public int InsertOrUpdate(LocalConfig localConfig)
        {
            int res = 0;
            bool exist = true;
            if (localConfig != null)
            {
                try
                {
                    Config? config = ConfigDao.Instance.FindLocal();
                    if (config == null)
                    {
                        exist = false;
                        config = new Config()
                        {
                            id = IdHelper.LongID,
                            clientId = App.InitConfig.clientId,
                            clientNumber = App.InitConfig.clientNumber,
                            upStatus = 0,
                            activeCode = App.Config.ActiveCode,
                            activeTime = App.Config.ActiveTime,
                            radomKey = App.Config.RadomKey,
                        };
                    }
                    config.activeCode = localConfig.ActiveCode;
                    config.activeTime = localConfig.ActiveTime;
                    config.radomKey = localConfig.RadomKey;
                    config.otherConfig = JsonHelper.ObjectToJson(App.Config.OtherConfig);
                    config.userConfig = JsonHelper.ObjectToJson(App.Config.UserConfig);
                    config.weighingConfig = JsonHelper.ObjectToJson(App.Config.WeighingConfig);
                    config.billPrintConfig = JsonHelper.ObjectToJson(App.Config.BillPrintConfig);
                    config.systemConfig = JsonHelper.ObjectToJson(App.Config.SystemConfig);

                    if (App.Config.ImgConfig.CapturePath.Contains("\\") || App.Config.ImgConfig.CapturePath.Contains("\\\\"))
                    {
                        string t = UrlHelper.Encode(App.Config.ImgConfig.CapturePath);
                        App.Config.ImgConfig.CapturePath = t;
                    }
                    config.imgConfig = JsonHelper.ObjectToJson(App.Config.ImgConfig);

                    config.functionConfig = JsonHelper.ObjectToJson(App.Config.FunctionConfig);

                    config.ledConfig = JsonHelper.ObjectToJson(App.Config.LedConfig);

                    config.functionConfig =JsonHelper.ObjectToJson(App.Config.FunctionConfig);
                    
                    res = exist ? ConfigDao.Instance.Sqlite.Update(config) : ConfigDao.Instance.Sqlite.Insert(config);
                    if (res <= 0)
                    {
                        Log.WriteLine("退出时，保存配制失败");
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("退出时，保存配制失败:" + ex.Message);
                }
            }
            return res;
        }
    }
}
